import { ROLE, PERMISSION, USER_INFO } from '../store/mutation-types'
import ls from '../utils/Storage'
/**
 * 操作权限校验
 * @param authConfig
 * @param permission
 * @param role
 * @param permissions
 * @param roles
 * @returns {boolean}
 */
const auth = function (authConfig, permission, permissions, role, roles) {
  const { roleLogical, permissionLogical } = authConfig
  return (
    checkFromPermissions(permissionLogical, permission, permissions) &&
    checkFromRoles(roleLogical, role, roles)
  )
}

/**
 * 检查权限是否有操作权限
 * @param check 需要检查的操作权限
 * @param permission 权限
 * @returns {boolean}
 */
const checkFromPermissions = function (permissionLogical, permission, permissions) {
  let access = true
  if (permission && permission.length > 0) {
    if (permissionLogical && permissionLogical === 'or') {
      access = permission.some((item) => {
        return permissions.includes(item)
      })
    } else {
      access = permission.every((item) => {
        return permissions.includes(item)
      })
    }
  }
  return access
}

/**
 * 检查 roles 是否有操作权限
 * @param check 需要检查的操作权限
 * @param roles 角色数组
 * @returns {boolean}
 */
const checkFromRoles = function (roleLogical, role, roles) {
  let access = true
  if (role && role.length > 0) {
    if (roleLogical && roleLogical === 'or') {
      access = role.some((item) => {
        return roles.includes(item)
      })
    } else {
      access = role.every((item) => {
        return roles.includes(item)
      })
    }
  }

  return access
}
const AuthorityPlugin = {
  install(Vue) {
    Vue.mixin({
      data() {
        return { authorize: {} }
      },
      beforeCreate() {
        this.$realm = {
          user: ls.get(USER_INFO),
          roles: ls.get(ROLE),
          permissions: ls.get(PERMISSION)
        }
      },
      created() {
        const config = this.$options.authorize
        if (config) {
          if (typeof config == 'string') {
            this.$api
              .GET(this.$options.authorize)
              .then((res) => {
                const authorize = {}
                const permissions = this.$realm.permissions
                const roles = this.$realm.roles
                res.data.forEach((item) => {
                  const { roleLogical, permissionLogical } = item
                  authorize[item.name] = auth(
                    { roleLogical: roleLogical, permissionLogical: permissionLogical },
                    item.permissions,
                    permissions,
                    item.roles,
                    roles
                  )
                })
                this.authorize = authorize
              })
              .catch((e) => {})
          } else {
            for (const key in config) {
              this.authorize[key] = {}
            }
            const permissions = this.$realm.permissions
            const roles = this.$realm.roles
            for (const key in config) {
              const authorize = {}
              this.$api
                .GET(config[key])
                .then((res) => {
                  res.data.forEach((item) => {
                    const { roleLogical, permissionLogical } = item
                    const name = item.name
                    authorize[name] = auth(
                      { roleLogical: roleLogical, permissionLogical: permissionLogical },
                      item.permissions,
                      permissions,
                      item.roles,
                      roles
                    )
                  })
                  this.authorize[key] = authorize
                })
                .catch((e) => {})
            }
          }
        }
      },
      methods: {
        $hasRole(role, logic) {
          const roles = this.$realm.roles
          return checkFromRoles(logic || 'and', Array.isArray(role) ? role : [role], roles)
        },
        $hasPermission(permission, logic) {
          const permissions = this.$realm.permissions
          return checkFromPermissions(
            logic || 'and',
            Array.isArray(permission) ? permission : [permission],
            permissions
          )
        }
      }
    })
  }
}

export default AuthorityPlugin
